package com.ruoyi.project.digital.controller.api;


import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.aspectj.lang.annotation.NoNeedAccess;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.digital.domain.*;
import com.ruoyi.project.digital.domain.dto.LoginWechatUser;
import com.ruoyi.project.digital.domain.enums.YesOrNoStatus;
import com.ruoyi.project.digital.service.*;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.service.ISysConfigService;
import io.rong.methods.user.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.GlobalConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(value = "我的相关接口", tags = "我的相关接口")
@RestController
@RequestMapping("api/front/myIndex")
public class MyIndexController extends BaseController {


    @Autowired
    private WechatUserTokenService wechatUserTokenService;
    @Autowired
    private ITUserService userService;
    @Autowired
    private IMoodService moodService;
    @Autowired
    private IPetService petService;
    @Autowired
    private IUserPetService userPetService;
    @Autowired
    private ITaskService taskService;
@Autowired
private ISysConfigService sysConfigService;

    @ApiOperation(value = "我的个人基本信息")
    @GetMapping("/info")
    private AjaxResult<TUser> getMyIndex() {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TUser user = userService.getById(loginMember.getId());
        return AjaxResult.success(user);
    }

    @ApiOperation(value = "初始化心情列表")
    @GetMapping("/moodList")
    @NoNeedAccess
    private AjaxResult<List<Mood>> moodList() {
        List<Mood> moodList = moodService.list();
        return AjaxResult.success(moodList);
    }

    @ApiOperation(value = "选择心情")
    @ApiImplicitParam(name = "id", required = true, value = "心情id")
    @GetMapping("/setMood/{id}")
    private AjaxResult setMood(@PathVariable Long id) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        userService.setMood(loginMember.getId(), id);
        return AjaxResult.success();
    }

    @ApiOperation(value = "当前心情对应的宠物")
    @GetMapping("/petDeafult")
    private AjaxResult<Pet> petDeafult() {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TUser user = userService.getById(loginMember.getId());
        Pet pet = petService.selectPetByMoodId(user.getMoodId());
        return AjaxResult.success(pet);
    }

    @ApiOperation(value = "初始化用户昵称")
    @ApiImplicitParam(name = "nickName", required = true, value = "昵称", paramType = "query")
    @GetMapping("/initUserNickName")
    private AjaxResult initUserNickName(@NotNull(message = "用户昵称不允许为空") String nickName) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TUser user = new TUser();
        user.setId(loginMember.getId());
        user.setNickName(nickName);
        userService.updateById(user);
        return AjaxResult.success();
    }


    @ApiOperation(value = "初始化当前宠物昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", required = true, value = "宠物昵称", paramType = "query"),
            @ApiImplicitParam(name = "petId", required = true, value = "宠物Id", paramType = "query")
    })
    @GetMapping("/initPetNickName")
    private AjaxResult initPetNickName(@NotNull(message = "宠物昵称不允许为空") String nickName, @NotNull(message = "宠物id不允许为空") Long petId) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        userPetService.initMessage(nickName, petId, loginMember.getId());
        return AjaxResult.success();
    }

    @ApiOperation(value = "初始化任务")
    @GetMapping("/initTask")
    @NoNeedAccess
    private AjaxResult<Task> initTask() {
       Task task=   taskService.getFirst();
        return AjaxResult.success(task);
    }

    @ApiOperation(value = "领取初始化任务")
    @GetMapping("/getFirstTask/{taskId}")
    private AjaxResult getFirstTask(@PathVariable @NotNull(message = "任务id不允许为空") Long taskId) {
       taskService.getFirstTask(taskId);
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TUser user= userService.getById(loginMember.getId());
        SysConfig sysConfig= sysConfigService.selectByConfigKey(Constants.KEY_VALUE);
        user.setStamina(Long.valueOf(sysConfig.getConfigValue()));
        user.setIsInit("1");
        userService.updateById(user);
        return AjaxResult.success(sysConfig.getConfigValue());
    }
}
